import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/components/Login.vue'  // 登录组件
import Content from '@/components/pax/Content.vue'  // 登录组件
import NavigationBar from '@/components/NavigationBar.vue'  // 登录组件
import Demo from '@/components/HttpApi.vue'    // 邮件组件

const routes = [
    {
        path: '/',
        redirect: '/home/pax'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: NavigationBar,
        children: [
            {
              path: 'pax',     // 子路由（商品详情，带参数）
              name: 'Pax',
              component: Content // 商品详情组件
            },
            {
                path: 'demo',     // 子路由（商品详情，带参数）
                name: 'Demo',
                component: Demo // 商品详情组件
              }
          ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})
// 🔒 全局前置守卫：检查登录状态
router.beforeEach((to, from, next) => {
    console.log("路由守卫触发:", to.path)  // 检查是否触发
    const haxpaxtoken = localStorage.getItem('pax_token')

    if (to.path === '/home/pax' && !haxpaxtoken) {
        next('/login') // 未登录访问 /mail → 跳转到 /login
    }
    if (to.path === '/login' && haxpaxtoken) {
        console.log('...............',haxpaxtoken)
        next('/home/pax') // 未登录访问 /mail → 跳转到 /login
    }
    else {
        next() // 其他情况正常跳转
    }
})
export default router